package com.dlc.felear.xiaoensale.utils;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.entity.CtConfig;
import com.dlc.felear.xiaoensale.entity.DisplayConfigInfo;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.OkHttpManager;
import com.dlc.felear.xiaoensale.helper.Session;
import com.itdlc.android.library.utils.APPDidUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.simple.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class GetValueFromServer {
    private static final String TAG = "GetValueFromServer";
    private Context context;
    private static GetValueFromServer instance;
    private Session session;

    public static GetValueFromServer getInstance(Context context) {
        if (instance == null) {
            synchronized (GetValueFromServer.class) {
                if (instance == null) {
                    instance = new GetValueFromServer(context);
                }
            }
        }
        return instance;
    }

    private GetValueFromServer(Context context) {
        this.context = context;
        session = Session.getSession();
    }

    public void updateInfo() {
        try {
            getCanteentId();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    private void getFuntion(String name) {

        OkHttpManager.HttpGet(HttpApi.FUNCTIONSTATUS+name, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, " Exception=" + e.getMessage());

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    Log.e(TAG, "大屏参数：" + response);
                    DisplayConfigInfo displayConfigInfo=JsonHelper.parserJson(response,DisplayConfigInfo.class);
                    if(displayConfigInfo!=null&&displayConfigInfo.getCode()==200){
                        Log.e(TAG,"gg="+JsonHelper.toJson(displayConfigInfo.getData()));
                        EventBus.getDefault().post(JsonHelper.toJson(displayConfigInfo.getData()),"DisplayConfigInfo_Config");
                    }

                } catch (Exception e) {
                    Log.e(TAG,e.getMessage());
                }
            }
        });
    }


    private void getCanteentId() {
        Log.e(TAG,"getCanteentId");
        Map<String, Object> temp = new HashMap<>();
        temp.put("deviceId", APPDidUtil.getAppDid());
        Log.e(TAG,"url="+HttpApi.CANTEENID);
        Log.e(TAG,"参数="+JsonHelper.toJson(temp));
        OkHttpManager.HttpPostJson(HttpApi.CANTEENID, temp, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "getThreadSleepTime Exception=" + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "获取餐厅运行商：" + response);
                CtConfig info = JsonHelper.parserJson(response, CtConfig.class);
                if (info != null && info.getCode() == 200) {
                    session.setString(Constant.CANTEENTID, info.getData().getOrgid());
                }
            }
        });
    }


}
